package com.bytedance.article.common.a.d;

import android.text.TextUtils;

import com.bytedance.framwork.core.monitor.HttpResponseException;
import com.bytedance.framwork.core.monitor.MonitorNetUtil$CompressType;

import org.json.JSONObject;

import java.util.LinkedList;

import inline.ScreenshotListener;
import inline.NetworkDemo;

public final class c implements com.bytedance.framwork.core.monitor.c.c$c {
    final class a {
        String a;
        String b;

        public a(String arg1, String arg2) {

            this.a = arg1;
            this.b = arg2;
        }
    }

    private volatile long a;
    private volatile int b;
    private volatile boolean c;
    private volatile long d;
    private volatile JSONObject e;
    private static final Object f = new Object();
    private static volatile c g;
    private final LinkedList h;
    private static volatile ScreenshotListener i;
    private volatile com.bytedance.article.common.a.d.a j;

    private c() {

        this.h = new LinkedList();
        com.bytedance.framwork.core.monitor.c.a(((com.bytedance.framwork.core.monitor.c.c$c) this));
        this.j = new com.bytedance.article.common.a.d.a();
    }

    public final void a(String arg6, String arg7, String arg8, boolean arg9) {
        Object v1_2;
        LinkedList v1_1;
        com.bytedance.article.common.a.d.a v0_2;
        int v4 = 10240;
        int v0 = 0;
        try {
            if (TextUtils.isEmpty(((CharSequence) arg6))) {
                return;
            }

            if (TextUtils.isEmpty(((CharSequence) arg7))) {
                return;
            }

            if (arg9) {
                JSONObject v1 = new JSONObject(arg7);
                v1.put("log_type", "log_exception");
                if (arg8 != null) {
                    if (arg8.length() > v4) {
                        v1.put("extraMessage", arg8.substring(0, 10240));
                    } else {
                        v1.put("extraMessage", arg8);
                    }
                }
            }

            if (com.bytedance.framwork.core.monitor.c.j() != null) {
                if (!android.support.design.a.m(arg6)) {
                    return;
                }

                if (this.c) {
                    return;
                }
                v1_2 = f;
                synchronized (v1_2) {
                    int v2 = this.h.size();
                    if (v2 >= 20) {
                        v0 = 1;
                    }

                    this.h.add(new a(arg6, arg7));
                    this.b = v2 + 1;
                }
                if (v0 == 0) {
                    return;
                }
                this.e();
                return;
            }

            if (this.j == null) {
                return;
            }

            v0_2 = this.j;
            if (v0_2.b) {
                return;
            }

            v1_1 = v0_2.a;
            synchronized (v1_1) {
                if (v0_2.a.size() > 40) {
                    v0_2.a.poll();
                }

                v0_2.a.add(new a(arg6, arg7));
                return;
            }
        } catch (Throwable v0_1) {
            v0_1.printStackTrace();
            return;
        }
    }

    public static c a() {
        if (g == null) {
            Object v1 = f;
            synchronized (v1) {
                if (g == null) {
                    g = new c();
                }
            }
        }

        return g;
    }

    static LinkedList a(c arg1) {
        return arg1.h;
    }

    static JSONObject a(c arg0, JSONObject arg1) {
        arg0.e = arg1;
        return arg1;
    }

    static void a(c arg6, String arg7, String arg8) {
        try {
            if (com.bytedance.framwork.core.monitor.c.j() == null) {
                return;
            }
            com.bytedance.framwork.core.monitor.c.j();
            NetworkDemo.a(com.bytedance.framwork.core.monitor.c.d(arg7), arg8.getBytes(), MonitorNetUtil$CompressType.GZIP, "application/json; charset=utf-8", true);
        } catch (Throwable v0) {
            int v1 = -1;
            int v0_1 = (v0 instanceof HttpResponseException) ? ((HttpResponseException) v0).getStatusCode() : v1;
            if (v0_1 < 500) {
                return;
            }

            if (v0_1 > 600) {
                return;
            }

            arg6.d = System.currentTimeMillis();
            arg6.c = true;
        }
    }

    static int b(c arg1) {
        arg1.b = 0;
        return 0;
    }

    public static boolean b() {
        boolean v0 = true;
        if (com.bytedance.framwork.core.monitor.c.j() != null && (com.bytedance.framwork.core.monitor.c.j().b("exception_filter_network"))) {
            v0 = false;
        }

        return v0;
    }

    static JSONObject c(c arg1) {
        return arg1.e;
    }

    public final void c() {
        try {
            if (this.j != null) {
                com.bytedance.article.common.a.d.a v0_1 = this.j;
                if (!v0_1.b) {
                    if (com.bytedance.framwork.core.monitor.c.j() != null) {
                        v0_1.b = true;
                    }

                    com.bytedance.frameworks.core.thread.c.a();
                    com.bytedance.frameworks.core.thread.c.a(new com.bytedance.article.common.a.d.b(v0_1));
                }
            }

            long v0_2 = System.currentTimeMillis();
            if (v0_2 - this.a > 1200000 && this.b > 0 || this.b > 20) {
                this.e();
            }

            if (!this.c) {
                return;
            }

            if (v0_2 - this.d <= 1800000) {
                return;
            }

            this.c = false;
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
    }

    static Object d() {
        return f;
    }

    private void e() {
        this.a = System.currentTimeMillis();
        com.bytedance.frameworks.core.thread.c.a();
        com.bytedance.frameworks.core.thread.c.a(new d(this));
    }
}

